package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.dao.*;
import com.sc.nft.entity.MerchantPledgeTaskCollectionConf;
import com.sc.nft.entity.MerchantPledgeTaskConf;
import com.sc.nft.entity.MerchantPledgeTaskIncomeConf;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.merchantPledgeTask.*;
import com.sc.nft.enums.MerchantPledgeTaskExamineStatusEnum;
import com.sc.nft.enums.MerchantPledgeTaskIncomeTypeEnum;
import com.sc.nft.enums.UserMerchantPledgeTaskStatusEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.AdminMerchantPledgeTaskConfService;
import com.sc.nft.service.UserCreatorInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class AdminMerchantPledgeTaskConfServiceImpl implements AdminMerchantPledgeTaskConfService {

    private final MerchantPledgeTaskConfDao merchantPledgeTaskConfDao;
    private final MerchantPledgeTaskCycleConfDao merchantPledgeTaskCycleConfDao;
    private final MerchantPledgeTaskIncomeConfDao merchantPledgeTaskIncomeConfDao;
    private final UserMerchantPledgeTaskRecordsDao userMerchantPledgeTaskRecordsDao;
    private final UserMerchantPledgeTaskCollectionRecordsDao userMerchantPledgeTaskCollectionRecordsDao;
    private final MerchantPledgeTaskCollectionConfDao merchantPledgeTaskCollectionConfDao;
    private final UserCreatorInfoService userCreatorInfoService;
    @Override
    public Page<AdminExaminePageVO> getExaminePage(String daoName, MerchantPledgeTaskExamineStatusEnum examineStatus, Integer pageNo, Integer pageSize) {
        return merchantPledgeTaskConfDao.getAdminExaminePage(new Page<>(pageNo, pageSize), daoName, examineStatus);
    }

    @Override
    public AminMerchantPledgeTaskConfDetailsVO getMerchantPledgeTaskConfDetails(Long id) {
        MerchantPledgeTaskConf pledgeTaskConf = merchantPledgeTaskConfDao.getById(id);
        Assert.notNull(pledgeTaskConf, ()->new GlobalRunTimeException("质押任务不存在"));
        AminMerchantPledgeTaskConfDetailsVO vo = BeanUtil.copyProperties(pledgeTaskConf, AminMerchantPledgeTaskConfDetailsVO.class);

        //如果默认的展示位置为空，则展示本dao
        if(ObjectUtil.isNull(pledgeTaskConf.getShowLocationDaoId())){
            vo.setShowLocationDaoId(pledgeTaskConf.getDaoId());
            vo.setShowLocationDaoName(pledgeTaskConf.getDaoName());
        }else {
            vo.setShowLocationDaoId(pledgeTaskConf.getShowLocationDaoId());
            vo.setShowLocationDaoName(userCreatorInfoService.getById(pledgeTaskConf.getShowLocationDaoId()).getDaoName());
        }

        List<AdminCycleConfVO> cycleConfVOList = merchantPledgeTaskCycleConfDao.getCycleListAndRewardsDetailsByTaskId(id);
        vo.setCycleConfVOList(cycleConfVOList);

        List<MerchantPledgeTaskIncomeConf> incomeConflist = merchantPledgeTaskIncomeConfDao.getListByTaskId(id);
        List<AdminIncomeConfVO> incomeVOlist = Lists.newArrayList();
        for (MerchantPledgeTaskIncomeConf merchantPledgeTaskIncomeConf : incomeConflist) {
            if(merchantPledgeTaskIncomeConf.getPledgeIncomeType() == MerchantPledgeTaskIncomeTypeEnum.DIRECT_REVENUE){
                List<AdminIncomeConfVO>  income =  merchantPledgeTaskIncomeConfDao.getDirectIncomeListAndRewardsDetailsByTaskId(id,merchantPledgeTaskIncomeConf.getId());
                incomeVOlist.addAll(income);
            }else if(merchantPledgeTaskIncomeConf.getPledgeIncomeType() == MerchantPledgeTaskIncomeTypeEnum.EXTRAPOLATED_RETURN){
                List<AdminIncomeConfVO>  income =  merchantPledgeTaskIncomeConfDao.getExtrapolatedIncomeListAndRewardsDetailsByTaskId(id,merchantPledgeTaskIncomeConf.getId());
                incomeVOlist.addAll(income);
            }
        }

        List<MerchantPledgeTaskCollectionConf> listByTaskConfId = merchantPledgeTaskCollectionConfDao.getListByTaskConfId(id);
        vo.setConfCollectionList(BeanUtil.copyToList(listByTaskConfId, PledgeTaskCollectionConfVO.class));
        vo.setIncomeConfList(incomeVOlist);

        return vo;
    }

    @Override
    public void examineMerchantPledgeTask(Long id, MerchantPledgeTaskExamineStatusEnum examineStatus,String reason) {
        MerchantPledgeTaskConf pledgeTaskConf = merchantPledgeTaskConfDao.getById(id);
        Assert.notNull(pledgeTaskConf, ()->new GlobalRunTimeException("质押任务不存在"));

        if(pledgeTaskConf.getExamineStatus() == MerchantPledgeTaskExamineStatusEnum.AUDIT_FAILURE){
            Assert.isFalse(StrUtil.isEmpty(reason), ()->new GlobalRunTimeException("审核失败原因不能为空"));
        }

        Assert.isTrue(pledgeTaskConf.getExamineStatus() == MerchantPledgeTaskExamineStatusEnum.UNDER_REVIEW, ()->new GlobalRunTimeException("审核状态错误"));
        if(examineStatus == MerchantPledgeTaskExamineStatusEnum.AUDIT_SUCCESS || examineStatus == MerchantPledgeTaskExamineStatusEnum.AUDIT_FAILURE){//审核成功
            Assert.isTrue(merchantPledgeTaskConfDao.updateExamineStatusById(pledgeTaskConf.getId(),examineStatus,reason),()->new GlobalRunTimeException("审核失败"));
        }else {
            throw new GlobalRunTimeException("审核状态错误");
        }
    }

    @Override
    public void updatePledgeTaskInfo(MerchantPledgeTaskConf merchantPledgeTaskConf) {
        Assert.isTrue(merchantPledgeTaskConf.getUserCount() <= merchantPledgeTaskConf.getUpperLimit(), ()->new GlobalRunTimeException("个人上限不能大于质押上限"));
        Assert.isTrue(merchantPledgeTaskConfDao.updatePledgeTaskInfo(merchantPledgeTaskConf),()->new GlobalRunTimeException("修改失败"));
    }

    @Override
    public Page<MerchantUserPledgeTaskPageVO> getMerchantUserPledgeTaskRecordPage(String daoName, String userTel, String pledgeName, UserMerchantPledgeTaskStatusEnum status, Integer pageNo, Integer pageSize,Boolean isFrozen) {
        return userMerchantPledgeTaskRecordsDao.getMerchantUserPledgeTaskRecordPage(daoName,userTel,pledgeName,status,pageNo,pageSize,isFrozen);
    }

    @Override
    public UserMerchantPledgeTaskRecordDetailsVO getMerchantUserPledgeTaskRecordDetails(Long id) {
        UserMerchantPledgeTaskRecordDetailsVO userPledgeTaskRecordDetailsVO = userMerchantPledgeTaskRecordsDao.userPledgeTaskRecordDetails(id);
        Long between = DateUtil.betweenDay(DateTime.now(), userPledgeTaskRecordDetailsVO.getEndTime(), Boolean.FALSE);
        userPledgeTaskRecordDetailsVO.setRemainingDays(between.intValue() <=0 ? 0: between.intValue());
        if(userPledgeTaskRecordDetailsVO.getStatus() == UserMerchantPledgeTaskStatusEnum.COMPLETED || userPledgeTaskRecordDetailsVO.getStatus() == UserMerchantPledgeTaskStatusEnum.ALREADY_ENDED){
            userPledgeTaskRecordDetailsVO.setRemainingDays(0);
        }

        userPledgeTaskRecordDetailsVO.setPledgeCollectionName(userMerchantPledgeTaskCollectionRecordsDao.getResultListByUserRecordsId(userPledgeTaskRecordDetailsVO.getId()));
        return userPledgeTaskRecordDetailsVO;
    }

    @Override
    public Page<UserPledgeTaskRwardsDetailsVO> getMerchantUserPledgeTaskRwardsDetails(Long id, Date startTime, Date endTime, int pageNo, int pageSize) {
        return userMerchantPledgeTaskRecordsDao.getMerchantUserPledgeTaskRwardsDetails(new Page<>(pageNo, pageSize),id,startTime,endTime);
    }

    @Override
    public List<MerchantPledgeDataExportPreviewVO> merchantPledgeDataExportPreview(String pledgeName, Long daoId) {
        return userMerchantPledgeTaskRecordsDao.merchantPledgeDataExportPreview(pledgeName,daoId);
    }

    @Override
    public List<AdminUserCreatorInfoAllDao> getAllDao(){
        return userCreatorInfoService.getAllByStatus();
    }

}
